package com.tanhua.server.service;

import com.tanhua.commons.templates.HuanXinTemplate;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.ContactVo;
import com.tanhua.domain.vo.MessageVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 即时通讯业务处理层
 */
@Service
@Slf4j
public class IMService {

    @Reference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private CommentApi commentApi;


    /**
     * 添加联系人
     */
    public void addContacts(Long friendId) {
        //当前登录用户id
        Long userId = UserHolder.getUserId();
        //1.往mongodb插入数据
        friendApi.addContacts(friendId,userId);
        //2.调用环信makeFriends方法
        huanXinTemplate.makeFriends(userId,friendId);
    }

    /**
     * 查询联系人列表
     * @param page
     * @param pagesize
     * @param keyword
     * @return
     */
    public PageResult<ContactVo> queryContacts(Integer page, Integer pagesize, String keyword) {
        PageResult<ContactVo> pageResult = new PageResult<>();
        //1.根据page pagesize keyword 条件分页查询联系人列表数据
        PageResult<Friend> friendPageResult = friendApi.queryContacts(page,pagesize,keyword,UserHolder.getUserId());
        if(friendPageResult == null || friendPageResult.getItems() == null || friendPageResult.getItems().size() == 0){
            return null;
        }
        List<Friend> listFriend = friendPageResult.getItems();
        //将List<Friend> 转为 List<ContactVo>
        List<ContactVo> contactVoList = new ArrayList<>();
        for (Friend friend : listFriend) {
            ContactVo contactVo = new ContactVo();
            Long friendId = friend.getFriendId();//好友id
            //2.根据friendIds到userInfo表中查询用户信息
            UserInfo userInfo = userInfoApi.findByUserId(friendId);
            BeanUtils.copyProperties(userInfo,contactVo);
            //contactVo.setId(Long.parseLong(friend.getId().toHexString()));//主键id处理
            contactVo.setUserId(userInfo.getId().toString());//好友id
            contactVoList.add(contactVo);
        }
        BeanUtils.copyProperties(friendPageResult,pageResult);
        pageResult.setItems(contactVoList);

        log.debug("***************"+pageResult.toString());
        //3.返回VO
        return pageResult;
    }


    /**
     * 喜欢列表 点赞列表 评论列表
     * //评论类型，1-点赞，2-评论，3-喜欢
     * @param page
     * @param pagesize
     * @param type
     * @return
     */
    public PageResult<MessageVo> messageCommentList(Integer page, Integer pagesize, int type) {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        PageResult<MessageVo> voPageResult = new PageResult<>();
        //1.根据type 和当前 用户id（动态发布者id）  page  pagesize
        PageResult<Comment> commentPageResult =  commentApi.messageCommentList(page,pagesize,type,userId);
        if(commentPageResult == null || commentPageResult.getItems() == null || commentPageResult.getItems().size() == 0){
            return null;
        }
        List<Comment> commentList = commentPageResult.getItems();
        //将List<Comment> 转为 List<MessageVo>
        List<MessageVo> messageVoList = new ArrayList<>();
        for (Comment comment : commentList) {
            MessageVo messageVo = new MessageVo();
            messageVo.setId(comment.getId().toHexString());//评论主键id
            String createDate = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(comment.getCreated()));
            messageVo.setCreateDate(createDate);//评论时间
            //2.根据评论者用户id 查UserInfo
            UserInfo userInfo = userInfoApi.findByUserId(comment.getUserId());
            BeanUtils.copyProperties(userInfo,messageVo);
            messageVoList.add(messageVo);
        }
        //3.封装Vo
        BeanUtils.copyProperties(commentPageResult,voPageResult);
        voPageResult.setItems(messageVoList);
        return voPageResult;
    }
}
